package handler

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	v1 "server_go/api/v1"
	"server_go/internal/service"
)

type TagHandler interface {
	AddTag(ctx *gin.Context)
	DeleteTag(ctx *gin.Context)
	UpdateTag(ctx *gin.Context)
	GetAllTag(ctx *gin.Context)
	GetTagById(ctx *gin.Context)
	GetAllTagList(ctx *gin.Context)
}

type tagHandler struct {
	*Handler
	tagService service.TagService
}

func NewTagHandler(handler *Handler, tagService service.TagService) TagHandler {
	return &tagHandler{
		Handler:    handler,
		tagService: tagService,
	}
}

// AddTag godoc
// @Summary 添加标签
// @Description 接收 tagName 字段，用于创建一个新标签
// @Tags Tag
// @Accept multipart/form-data
// @Produce json
// @Param tagName formData string true "标签名称"
// @Success 200 {object} v1.Response "成功"
// @Router /admin/tag [post]
func (h *tagHandler) AddTag(ctx *gin.Context) {
	// 从form中获取tagName
	req := new(v1.AddTagRequest)
	if err := ctx.ShouldBind(&req); err != nil {
		h.logger.Error("params error", zap.Any("error", err))
		v1.HandleError(ctx, http.StatusBadGateway, v1.ErrBadGateway, nil)
		return
	}
	h.logger.Info("AddTag Information")
	if err := h.tagService.AddTag(req); err != nil {
		h.logger.Error("AddTag error", zap.String("tagName", req.Name))
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)

}

// DeleteTag godoc
// @Summary 删除标签
// @Description 删除标签
// @Tags 标签
// @Accept application/json
// @Produce application/json
// @Param tagId path int true "标签ID"
// @Success 200 {object} v1.Response{data=v1.DeleteTagResponse}
// @Router /admin/tag/{tagId} [delete]
func (h *tagHandler) DeleteTag(ctx *gin.Context) {
	// 从 uri解析字段
	req := new(v1.DeleteTagRequest)
	if err := ctx.ShouldBindUri(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadGateway, v1.ErrBadGateway, nil)
		return
	}
	h.logger.Info("DeleteTag Information")
	resp, err := h.tagService.DeleteTag(req)
	if err != nil {
		h.logger.Error("DeleteTag error", zap.Any("error", err))
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, resp)
}

// UpdateTag godoc
// @Summary 更新标签
// @Description 更新标签
// @Tags 标签
// @Accept multipart/form-data
// @Produce json
// @Param id path int true "标签ID"
// @Param UpdateTagRequest body v1.UpdateTagRequest true "更新标签"
// @Success 200 {object} v1.Response "更新成功"
// @Route /admin/tag/{tagId} [put]
func (h *tagHandler) UpdateTag(ctx *gin.Context) {
	req := new(v1.UpdateTagRequest)
	if err := ctx.ShouldBindUri(&req); err != nil {
		h.logger.Error("path params error")
		v1.HandleError(ctx, http.StatusBadGateway, v1.ErrBadGateway, nil)
		return
	}
	// 从form中获取tagName
	if err := ctx.ShouldBind(&req); err != nil {
		h.logger.Error("form params error")
		v1.HandleError(ctx, http.StatusBadGateway, v1.ErrBadGateway, nil)
		return
	}
	h.logger.Info("UpdateTag Information", zap.Any("req", req))
	if err := h.tagService.UpdateTag(req); err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, nil)
}

// GetAllTag godoc
// @Summary 获取所有标签
// @Description 获取所有标签
// @Tags 标签
// @Accept  json
// @Produce  json
// @Param page query int true "页码" default(1)
// @Param pageSize query int true "每页数量" default(10)
// @Success 200 {object} v1.Response "返回所有标签"
// @Route /admin/tags [get]
func (h *tagHandler) GetAllTag(ctx *gin.Context) {
	req := new(v1.GetTagRequest)
	if err := ctx.ShouldBind(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadGateway, v1.ErrBadGateway, nil)
		return
	}
	h.logger.Info("GetAllTag Information")
	resp, err := h.tagService.GetAllTag(req)
	//h.logger.Info("GetallTag data", zap.Any("data", resp))
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, resp)
}

func (h *tagHandler) GetTagById(ctx *gin.Context) {
	req := new(v1.GetTagByIdRequest)
	if err := ctx.ShouldBindUri(&req); err != nil {
		v1.HandleError(ctx, http.StatusBadGateway, v1.ErrBadGateway, nil)
		return
	}
	h.logger.Info("GetTagById Information")
	resp, err := h.tagService.GetTagById(req)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}
	v1.HandleSuccess(ctx, resp)
}

func (h *tagHandler) GetAllTagList(ctx *gin.Context) {
	// 没有参数绑定

	h.logger.Info("GetAllTagList")

	resp, err := h.tagService.GetAllTagList()

	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
		return
	}

	v1.HandleSuccess(ctx, resp)
}
